<?php
header('Access-Control-Allow-Origin:*');  //支持全域名访问，不安全，部署后需要固定限制为客户端网址
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http 动作
header('Access-Control-Allow-Headers:x-requested-with,content-type');  //响应头 请按照自己需求添加。
$conn = mysqli_connect("localhost","root","","fruit");
$conn->query("set names utf8");
$type=$_GET['type'];
$keyword=$_GET["keyword"];
switch ($type) {
	case 'kw':
		$sql1 = "SELECT * FROM product WHERE name like '%{$keyword}%'";
		$ret1 = $conn->query($sql1);

		$arr1 = array();
		while ($row1 = $ret1->fetch_assoc()) {
			$arr1[] = $row1;
		}
		$str1 = json_encode($arr1);
		echo '{"data":'.$str1.'}';

		break;
		case 'salse':
			# code...

		$sql1 = "SELECT * FROM product WHERE name like '%{$keyword}%' ORDER BY sales DESC";
		$ret1 = $conn->query($sql1);

		$arr1 = array();
		while ($row1 = $ret1->fetch_assoc()) {
			$arr1[] = $row1;
		}
		$str1 = json_encode($arr1);
		echo '{"data":'.$str1.'}';
			break;
		case 'priceLowHigh':
			# code...

		$sql1 = "SELECT * FROM product WHERE name like '%{$keyword}%' ORDER BY price ASC";
		$ret1 = $conn->query($sql1);

		$arr1 = array();
		while ($row1 = $ret1->fetch_assoc()) {
			$arr1[] = $row1;
		}
		$str1 = json_encode($arr1);
		echo '{"data":'.$str1.'}';
			break;
		case 'priceHighLow':
			# code...

		$sql1 = "SELECT * FROM product WHERE name like '%{$keyword}%' ORDER BY price DESC";
		$ret1 = $conn->query($sql1);

		$arr1 = array();
		while ($row1 = $ret1->fetch_assoc()) {
			$arr1[] = $row1;
		}
		$str1 = json_encode($arr1);
		echo '{"data":'.$str1.'}';
			break;
	
	default:
		# code...
		break;
}

?>
